package com.example.flutteruikit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtil {

    public static final SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    public static final SimpleDateFormat FORMAT_MIN = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
    public static final SimpleDateFormat FORMAT_YEAR = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    public static final SimpleDateFormat FORMAT_DAY = new SimpleDateFormat("MM-dd", Locale.CHINA);
    public static final SimpleDateFormat FORMAT_SECOND = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
    public static final SimpleDateFormat FORMAT_HOUR = new SimpleDateFormat("HH:mm", Locale.CHINA);

    /**
     * TIME_DIFFERENCE = 服务器时间 - 本机时间
     */
    private static volatile long TIME_DIFFERENCE = 0;

    /**
     * 设置机器与服务器时间差
     */
    public static void setTimeDifference(long dif) {
        TIME_DIFFERENCE = dif;
    }

    /**
     * 计算服务器时间与本机时间差，并存放在临时变量
     *
     * @param date   服务器时间
     * @param format 时间格式
     */
    public static long setServiceDate(String date, SimpleDateFormat format) {
        try {
            long serviceLong = format.parse(date).getTime();
            TIME_DIFFERENCE = serviceLong - new Date().getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return TIME_DIFFERENCE;
    }

    /**
     * 获取当前时间的字符串
     */
    public static String longToDate(long time, SimpleDateFormat format) {
        return format.format(new Date(time));
    }

    /**
     * 将时间字符串转为long值
     */
    public static long dateToLong(String time, SimpleDateFormat format) {
        Date date = new Date();
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /**
     * 获取当前时间long值
     */
    public static long nowTimeLong() {
        return new Date().getTime() + TIME_DIFFERENCE;
    }

    /**
     * 判断两个时间
     *
     * @param start 开始时间
     * @param end   结束时间
     * @return true: start <= end; false: start > end
     */
    public static boolean judgmentTime(String start, String end, SimpleDateFormat format) {
        Calendar calendarStart = Calendar.getInstance();
        Calendar calendarEnd = Calendar.getInstance();
        try {
            calendarStart.setTime(format.parse(start));
            calendarEnd.setTime(format.parse(end));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendarStart.before(calendarEnd);
    }

    /**
     * 计算与当前的时间差
     */
    public static long currentDifference(String date, SimpleDateFormat format) {
        return dateToLong(date, format) - new Date().getTime() - TIME_DIFFERENCE;
    }

    /*
     *计算time2减去time1的差值 差值只设置 几天 几个小时 或 几分钟
     * 根据差值返回多长之间前或多长时间后
     * */
    public static int getDistanceTime(long time1, long time2) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long diff;

        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        return (int) sec;
    }

    /*
    获取今天零点的时间戳(毫秒)
     */
    public static long ZeroTime() {
        long current = System.currentTimeMillis();
        return current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
    }
}
